import { defineStore } from 'pinia';
import { useMenuStore } from './menu';
import { api } from 'src/boot/axios';
export const apiFns = {
  getLoginUser: () => api.get('/api/auth/getLoginUser'),
};
export const useAuthStore = defineStore({
  id: 'auth',
  state: () => ({
    user: null,
  }),
  actions: {
    init(router) {
      // 初始化逻辑
      return new Promise((resolve, reject) => {
        apiFns
          .getLoginUser()
          .then((res) => {
            const menuStore = useMenuStore();
            this.setUser(res.data);
            menuStore.init(router, resolve);
          })
          .catch(() => {
            reject();
          });
      });
    },
    setUser(user) {
      this.user = user;
    },
    clearUser() {
      this.user = {};
    },
  },
});
